<template>
  <div class="wrapper">
    <el-card class="box-card" v-for="(item,index) in List.reverse()" :key="index">
      <el-breadcrumb separator="|">
        <el-breadcrumb-item><span class="author">{{item.author}}</span></el-breadcrumb-item>
        <el-breadcrumb-item><span class="time">{{item.time}}</span></el-breadcrumb-item>
        <el-breadcrumb-item><span class="type" v-for="(it,ind) in item.type" :key="ind" style="margin-right:.1rem">{{type[it]}}</span></el-breadcrumb-item>
        <div style="display:flex;align-items:center">
          <img class="look" src="@/assets/look.png" />{{item.look}}
          <img class="comment" src="@/assets/comment.png">{{item.comment}}
          <img class="likes" src="@/assets/likes.png">{{item.likes}}
        </div>
      </el-breadcrumb>
      <div class="title">{{item.title}}</div>
      <!-- <span class="description">{{item.description}}</span> -->
      <div v-html="markdown(item.description)" class="description"></div>
      <a href="javascript:;" class="color-btn read" @click.prevent="read(item)">阅读全文</a>
    </el-card>
    <el-backtop />
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue'
import { getArticles } from '../api/index'
import { Star, Comment } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js'
import { useStore } from 'vuex'
// 被点击的文章
interface interfaceRead {
  author: string
  comment: number
  content: string
  createdBy: number
  description: string
  id: number
  likes: number
  look: number
  time: string
  title: string
  type: string
}
export default defineComponent({
  setup() {
    let List = ref([])
    const type = ref(['', '前端', '后端', 'Android', 'iOS', '算法', '开发工具'])

    const store = useStore()
    const get_Articles = (e?: string) => {
      getArticles(e).then((res) => {
        List.value = res.data.data as string[]
        store.commit('getList', List.value)
      })
    }

    const router = useRouter()
    const read = (item: interfaceRead): void => {
      router.push('/read/' + item.title)
      localStorage.setItem('nowArticles_id', item.id.toString())
    }

    const markdown = (target) => {
      const md = new MarkdownIt({
        highlight: function (str, lang) {
          if (lang && hljs.getLanguage(lang)) {
            try {
              return (
                '<pre class="hljs"><code>' + hljs.highlight(lang, str, true).value + '</code></pre>'
              )
            } catch (_) {
              _
            }
          }
          return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>'
        },
      })
      const result = md.render(target)
      return result
    }

    onMounted(() => {
      get_Articles('0')
    })
    return {
      List,
      type,
      Star,
      Comment,
      get_Articles,
      read,
      markdown,
    }
  },
})
</script>

<style lang="scss" scoped>
.wrapper {
  // overflow: hidden;
  width: 100%;
  margin-top: 1rem;
}
.box-card {
  position: relative;
  width: 100%;
  margin: 0.3rem 0;
  .author {
    color: #4e5969;
  }
  .time,
  .type {
    color: #86909c;
  }
}
.title,
.el-breadcrumb {
  margin-bottom: 0.5rem;
}
.title {
  font-weight: 700;
  font-size: 1rem;
  color: #1d2129;
  width: 100%;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
}
.description {
  font-weight: 400;
  font-size: 13px;
  color: #86909c;
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}
img {
  width: 1rem;
  height: 1rem;
  margin: 0 0.1rem 0 0.4rem;
}
img:first-child {
  width: 1rem;
  height: 1rem;
  margin: 0 0.1rem 0 0;
}
.status {
  display: flex;
  align-items: center;
}
.read {
  position: absolute;
  top: 35%;
  right: 2rem;
}
.markdown-body {
  padding: 20px;
  min-width: 200px;
  // max-width: 900px;
  font-size: 12px;
  h2 {
    font-size: 18px;
    margin: 1em 0 15px;
    padding-top: 0.8em;
    padding-bottom: 0.8em;
  }
  h3 {
    font-size: 14px;
    margin: 22px 0 16px;
  }
  h4 {
    font-size: 13px;
    margin: 20px 0 16px;
  }
  h5 {
    font-size: 12px;
    margin: 16px 0 16px;
    font-weight: 700;
  }
  p {
    font-size: 12px;
    line-height: 24px;
    color: #666666;
    margin-top: 0px;
    margin: 8px 0;
    margin: 14px 0 14px;
  }
  pre {
    background-color: #eee;
    margin-bottom: 8px;
    margin-top: 8px;
    margin: 12px 0 12px;
  }
  blockquote {
    margin-bottom: 8px;
    margin-top: 8px;
    margin: 14px 0 14px;
    background-color: #eee;
    padding: 16px 16px;
  }
  tr {
    background-color: #f5f5f5;
  }
  code {
    background-color: #eee;
  }
  ul,
  ol,
  li {
    list-style: unset;
    font-size: 12px;
    line-height: 20px;
    color: #666666;
    margin-top: 0px;
    margin: 8px 0;
  }
  blockquote {
    border-color: #48b6e2;
  }
  table {
    display: table;
    width: 100%;
    max-width: 100%;
    margin-bottom: 20px;
  }
}
</style>